package com.carefree.system.modules.admin.controller;

import com.carefree.common.core.PageUtils;
import com.carefree.common.core.Result;
import com.carefree.common.emus.BusinessTypeEnum;
import com.carefree.common.emus.OperatorTypeEnum;
import com.carefree.common.valid.AddGroup;
import com.carefree.common.valid.UpdateGroup;
import com.carefree.system.common.annotation.SystemLog;
import com.carefree.system.modules.admin.entity.PostEntity;
import com.carefree.system.modules.admin.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 岗位
 *
 * @author tengbin
 */
@Api(tags = "岗位管理")
@RestController
@RequestMapping("/sys/post")
public class PostController {

    @Resource
    private PostService postService;


    /**
     * 列表
     *
     * @param params 分页参数
     * @return
     */
    @SystemLog(value = "分页查询岗位信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "分页查询岗位信息")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('btn:post:list')")
    public Result<PageUtils> list(@RequestParam Map<String, Object> params) {
        PageUtils page = postService.queryPage(params);
        return Result.success(page);
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @SystemLog(value = "根据id查询岗位信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id查询岗位信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('btn:post:list')")
    public Result<PostEntity> info(@PathVariable("id") Long id) {
        PostEntity post = postService.getById(id);
        return Result.success(post);
    }

    /**
     * 保存
     *
     * @param Post 岗位
     * @return
     */
    @SystemLog(value = "保存岗位信息", businessType = BusinessTypeEnum.INSERT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "保存岗位信息")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('btn:post:add')")
    public Result<Object> save(@Validated({AddGroup.class}) @RequestBody PostEntity post) {
        return postService.save(post) ? Result.ok() : Result.error();
    }

    /**
     * 修改
     *
     * @param Post 岗位
     * @return
     */
    @SystemLog(value = "修改岗位信息", businessType = BusinessTypeEnum.UPDATE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "修改岗位信息")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('btn:post:update')")
    public Result<Object> update(@Validated({UpdateGroup.class}) @RequestBody PostEntity post) {
        return postService.updateById(post) ? Result.ok() : Result.error();
    }

    /**
     * 启用或禁用
     *
     * @param id
     * @return
     */
    @SystemLog(value = "根据id启用或禁用岗位", businessType = BusinessTypeEnum.STATUS, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id启用或禁用岗位")
    @GetMapping("/active/{id}/{status}")
    @PreAuthorize("hasAuthority('btn:post:enable')")
    public Result<Object> activeOrInActiveById(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return postService.activeOrInActiveById(id, status) ? Result.ok() : Result.error();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @SystemLog(value = "根据id批量删除岗位信息", businessType = BusinessTypeEnum.DELETE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id批量删除岗位信息")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('btn:post:remove')")
    public Result<Object> delete(@RequestBody Long[] ids) {
        return postService.removeByIds(Arrays.asList(ids)) ? Result.ok() : Result.error();
    }

}
